package com.gxuwz.medical.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gxuwz.medical.database.DBUtil;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.register.Register;
import com.gxuwz.medical.domain.vo.Page;

/**
 * 参合登记管理
 * @author 沉羽墨玖
 *
 */
public class RegisterDao extends BaseDao {
	private static final String SQL_SELECT_REGID = "select * from register where regID=?";

	@Override
	protected Register handle(ResultSet rs) throws Exception {
		try {
			Register register = new Register();
			register.setPersCode(rs.getString("persCode"));
			register.setFamily(new Family(rs.getString("famCode")));
			register.setPersName(rs.getString("persName"));
			register.setCardId(rs.getString("cardId"));
			register.setSex(rs.getString("sex"));
			register.setAge(rs.getInt("age"));
			register.setIllName(rs.getString("illName"));
			register.setLiveAddress(rs.getString("liveAddress"));
			register.setTelephone(rs.getString("telephone"));
			register.setCreateTime(rs.getString("createTime"));
			register.setInvoice(rs.getString("invoice"));
			return register;
		} catch (SQLException e) {
			throw new SQLException("结果集转换为对象失败:", e);
		}
	}

	/**
	 * 查找对应的参合登记记录并转为对象实例
	 * 
	 * @param areacode
	 * @return
	 * @throws Exception
	 */
	public Register get(String regID) throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement(SQL_SELECT_REGID);
			int index = 1;
			pstmt.setString(index, regID);
			rs = pstmt.executeQuery();
			return handle(rs);
		} catch (SQLException e) {
			throw new SQLException("读取regID记录失败", e);
		}
	}

	/**
	 * @param sql
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public List<Register> queryObjects(String sql, Object[] params)
			throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<Register> results = new ArrayList<Register>();
		try {
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement(sql);
			int index = 1;
			if (params != null) {
				for (Object param : params) {
					if (param instanceof String) {
						pstmt.setString(index++, (String) param);
					}
					if (param instanceof Integer) {
						pstmt.setInt(index++, (Integer) param);
					}
					if (param instanceof Float) {
						pstmt.setFloat(index++, (Float) param);
					}
				}

			}
			rs = pstmt.executeQuery();
			while (rs.next()) {
				Register register = handle(rs);
				results.add(register);
			}
			return results;
		} catch (SQLException e) {
			throw new SQLException("执行SQL[" + sql + "]失败", e);
		}
	}

	/**
	 * 分页
	 * 
	 * @param select
	 *            * from institution where areacode=?
	 * @param params
	 * @param page
	 * @param size
	 * @return
	 * @throws Exception
	 */
	public Page queryOjectsByPage(String sql, Object[] params, int pageNo,
			int size) throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Page page = null;
		List<Register> datas = new ArrayList<Register>();
		try {
			int start = (pageNo - 1) * size;
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement(sql + " limit " + start + "," + size);
			int index = 1;
			if (params != null) {
				for (Object param : params) {
					if (param instanceof String) {
						pstmt.setString(index++, (String) param);
					}
					if (param instanceof Integer) {
						pstmt.setInt(index++, (Integer) param);
					}
					if (param instanceof Float) {
						pstmt.setFloat(index++, (Float) param);
					}
				}
			}
			rs = pstmt.executeQuery();
			while (rs.next()) {
				Register register = handle(rs);
				datas.add(register);
			}
			// 统计SQL对应记录数
			int total = count(sql, params);
			page = new Page(total, pageNo, size, datas);
			return page;
		} catch (SQLException e) {
			throw new SQLException("执行SQL[" + sql + "]失败", e);
		}

	}

}
